<?php

	class Visual {

		private $loader, $twig;
		private $vars = array();

		public function __construct() {
			$template_path          = TEMPLATES_PATH;
			$template_compiled_path = TEMPLATES_COMPILED_PATH;

			if ($template_compiled_path && !file_exists($template_compiled_path)) {
				mkdir($template_compiled_path, 0777);
			}

			$this->loader = new Twig_Loader_Filesystem($template_path);
			$this->twig   = new Twig_Environment($this->loader, array(
				'cache'      => $template_compiled_path,
				'debug'      => TEMPLATES_DEBUG,
				'autoescape' => false,
			));

			$this->vars['_get']            = $_GET;
			$this->vars['_post']           = $_POST;
			$this->vars['_request']        = $_REQUEST;
			$this->vars['_cookie']         = $_COOKIE;
			$this->vars['_back_url']       = urlencode($_SERVER['REQUEST_URI']);
			$this->vars['_debug']          = DEBUG_MODE;
			$this->vars['_session']        = $_SESSION;
			$this->vars['CURRENCY_SYMBOL'] = CURRENCY_SYMBOL;

			// OTHER DEFINES
			$this->vars['_permis']      = Auth::currPermis();
			$this->vars['_permis_tree'] = Auth::permis2tree();
			$this->vars['_settings']    = Auth::currSettings();
			$this->vars['_login']       = Auth::currLogin();
			$this->vars['_box_count']   = Model_Box::getTotalCount();
			$this->vars['_store_cost']  = Model_Store::getStoreCost();
		}

		public function assign($name, $value) {
			$this->vars = array_merge($this->vars, array($name => $value));
		}

		public function display($tpl) {
			$template = $this->twig->loadTemplate($tpl);
			echo $template->render($this->vars);
		}

		public function fetch($tpl) {
			$template = $this->twig->loadTemplate($tpl);

			return $template->render($this->vars);
		}

		public function getVars($key = null) {
			return is_null($key) ? $this->vars : $this->vars[$key];
		}

	}
